﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Configuration;

namespace ShowWindowIntelligence
{
    public partial class AuthenticationForm : Form
    {
        #region Win32 APIs

        private const int SW_SHOWNOACTIVATE = 4;

        [DllImport("shell32.dll")]
        static extern IntPtr ShellExecute(
                            IntPtr hwnd,
                            string lpOperation,
                            string lpFile,
                            string lpParameters,
                            string lpDirectory,
                            int nShowCmd);
        #endregion

        public AuthenticationForm()
        {
            InitializeComponent();
        }

        public string Authenticcode
        {
            get { return textBoxAuthenticcode.Text.Trim(); }
        }

        private void OnGettingAuthenticodeClick(object sender, EventArgs e)
        {
            OpenBrowser(string.Format(MyAppInfo.AuthenticodeRequestURL, MyAppInfo.AppKey));
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            e.Cancel = !VerifyDialog();
 
        }
        private bool VerifyDialog()
        {
            if (string.IsNullOrEmpty(Authenticcode))
            {
                MessageBox.Show(SR.AuthenticcodeEmpty, SR.WarningDialogTitle, MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        private static void OpenBrowser(string url)
        {
            try
            {
                ShellExecute(IntPtr.Zero, "open", url, string.Empty, string.Empty, SW_SHOWNOACTIVATE);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception when creating Internet Explorer object {0}", e);
                return;
            }
        }
    }
}
